

const mysql2 = require('mysql2');
const fs = require("node:fs")
const jsyaml = require("js-yaml")
const express = require('express');

const yaml = fs.readFileSync("./db.config.yaml", "utf8");
const config = jsyaml.load(yaml);
const sql = mysql2.createConnection({
  ...config.db
});


var app = express();
app.use(express.json());

app.get("/", (req, res) => {
  sql.query(
    "SELECT * FROM `users`",
    function (err, results, fields) {
      console.log(results);
      res.send(results)
    }
  )
})

app.get("/", (req, res) => {
  sql.query(
    "SELECT * FROM `shops`",
    function (err, results, fields) {
      console.log(results);
      res.send(results)
    }
  )
})

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
